package org.gongliang.jsmile.common.kit.web;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.DeviceType;
import eu.bitwalker.useragentutils.UserAgent;

/**
 * 用户浏览器工具类
 * 
 * @author 龚亮
 */
public class UserAgentKit {

	private static final Logger logger = LoggerFactory.getLogger(UserAgentKit.class);

	/**
	 * 获取用户代理对象
	 * 
	 * @param request
	 * @return
	 */
	public static UserAgent getUserAgent(HttpServletRequest request) {
		return UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
	}

	/**
	 * 获取用户浏览器
	 * 
	 * @param request
	 * @return
	 */
	public static String getUserBrowser(HttpServletRequest request) {
		UserAgent userAgent = getUserAgent(request);
		String browser = "";
		if (userAgent != null) {
			try {
				browser = userAgent.getBrowser().getName();
			} catch (Exception e) {
				logger.error("获取用户浏览器版本号失败!");
			}
		}
		return browser;
	}

	/**
	 * 获取用户浏览器版本号
	 * 
	 * @param request
	 * @return
	 */
	public static String getUserBrowserVersion(HttpServletRequest request) {
		UserAgent userAgent = getUserAgent(request);
		String browserVersion = "";
		if (userAgent != null) {
			try {
				browserVersion = userAgent.getBrowserVersion().getVersion();
			} catch (Exception e) {
				logger.error("获取用户浏览器版本号失败!");
			}
		}
		return browserVersion;
	}

	/**
	 * 获取用户操作系统
	 * 
	 * @param request
	 * @return
	 */
	public static String getUserOs(HttpServletRequest request) {
		UserAgent userAgent = getUserAgent(request);
		String os = "";
		if (userAgent != null) {
			try {
				os = userAgent.getOperatingSystem().getName();
			} catch (Exception e) {
				logger.error("获取用户浏览器版本号失败!");
			}
		}
		return os;
	}

	/**
	 * 获取设备类型
	 * 
	 * @param request
	 * @return
	 */
	public static DeviceType getDeviceType(HttpServletRequest request) {
		return getUserAgent(request).getOperatingSystem().getDeviceType();
	}

	/**
	 * 是否是PC
	 * 
	 * @param request
	 * @return
	 */
	public static boolean isComputer(HttpServletRequest request) {
		return DeviceType.COMPUTER.equals(getDeviceType(request));
	}

	/**
	 * 是否是手机
	 * 
	 * @param request
	 * @return
	 */
	public static boolean isMobile(HttpServletRequest request) {
		return DeviceType.MOBILE.equals(getDeviceType(request));
	}

	/**
	 * 是否是平板
	 * 
	 * @param request
	 * @return
	 */
	public static boolean isTablet(HttpServletRequest request) {
		return DeviceType.TABLET.equals(getDeviceType(request));
	}

	/**
	 * 是否是手机和平板
	 * 
	 * @param request
	 * @return
	 */
	public static boolean isMobileOrTablet(HttpServletRequest request) {
		DeviceType deviceType = getDeviceType(request);
		return DeviceType.MOBILE.equals(deviceType) || DeviceType.TABLET.equals(deviceType);
	}

	/**
	 * 获取浏览类型
	 * 
	 * @param request
	 * @return
	 */
	public static Browser getBrowser(HttpServletRequest request) {
		return getUserAgent(request).getBrowser();
	}

	/**
	 * 是否IE版本是否小于等于IE8
	 * 
	 * @param request
	 * @return
	 */
	public static boolean isLteIE8(HttpServletRequest request) {
		Browser browser = getBrowser(request);
		return Browser.IE5.equals(browser) || Browser.IE6.equals(browser) || Browser.IE7.equals(browser)
				|| Browser.IE8.equals(browser);
	}

}
